<?php
/**
 * Created by PhpStorm.
 * User: zhaoyang
 * Date: 18/9/18
 * Time: 下午3:09
 */
$api_service_path = __DIR__ . "/../../../api/";
ob_start();
require_once($api_service_path . "public/index.php");
ob_end_clean();
DB::connection()->disableQueryLog(); 
//以2开头，9位自增长数字，
$data_si_list=DataSupplyItem::select('id')->whereNull("anhui_code")->orderBy("id");
$data_max = DB::table('anhui_data.supply_item')->max('anhui_code');
if($data_max < 200000000) $data_max = 200000000;
$update_sql = [];
$data_si_list->chunk(1000, function($silist) use (&$data_max,&$update_sql){       
    foreach ($silist as $si) {
        $data_max = $data_max+1;
        $update_sql [$si->id]= "update anhui_data.supply_item set anhui_code='".$data_max."' where id=".$si->id.";";
    }
    echo "ERP安惠编码正在生成,请稍等...\r\n";
});

foreach ( $update_sql as $k=>$val) {
    DB::update($val);
    Log::info("$k ERP安惠编码已经生成"); 
    echo "$k ERP安惠编码已经生成\r\n";
}
Log::info("ERP安惠编码已经生成"); 
echo "ERP安惠编码已经生成\r\n";
sleep(3);
//以5开头，9位自增长数字，
$si_list=SupplyItem::select('id','erp_id','type')->whereNull("anhui_code")->orderBy("id");
$max = DB::table('anhui.supply_item')->max('anhui_code');
if($max < 500000000) $max = 500000000;
$update_sql = [];
$si_list->chunk(1000, function($silist) use(&$max,&$update_sql){              
    foreach ($silist as $si) {
        if ($si->type!=0) {
            $max = $max+1;
            $si->anhui_code = $max;
        }else{
            $data_anhui = DataSupplyItem::select('id','anhui_code')->where("id",$si->erp_id)->first();
            $si->anhui_code = $data_anhui->anhui_code;
        }
        $update_sql [$si->id]= "update anhui.supply_item set anhui_code='".$si->anhui_code."' where id=".$si->id.";";
    }
    echo "CMS安惠编码正在生成,请稍等...\r\n";
}); 

foreach ( $update_sql as $k=>$val) {
    DB::update($val);
    Log::info("$k CMS安惠编码已经生成"); 
    echo "$k CMS安惠编码已经生成\r\n";
}
Log::info("CMS安惠编码已经生成"); 
echo "CMS安惠编码已经生成\r\n";